'''
窗口父类
'''
import pygame,sys,os
from music_manager import MusicManager
from Person import Person
from Door import Door
from Door_Person import Door_Person
class MainWindow():
    def __init__(self):
        self.window = None
        #屏幕大小
        self.Screen_width = 1000
        self.Screen_height = 800

        self.BackGroundColor = "#FFECA1"

        self.window = pygame.display.set_mode((self.Screen_width, self.Screen_height))  # 窗口大小
        self.clock = pygame.time.Clock()  # 控制游戏帧率

        self.music_manager = MusicManager()
        self.mute_btn = pygame.Rect(20, 20, 40, 40)  # 静音按钮位置
        self.mute_icon = pygame.image.load(self.resource_path("source/img/music_b.png"))  # 正常图标
        self.unmute_icon = pygame.image.load(self.resource_path("source/img/music_j.png"))  # 静音图标

        self.person = Person(0, 0)  # 人物初始化
        self.S_Walls = []  # 静态墙初始化 [x,y]
        self.D_Walls = []
        self.Spikes = []
        self.One_spike = []
        self.Many_spike = []
        self.Gear = []
        self.obstacles = pygame.sprite.Group()
        self.D_obstacles = pygame.sprite.Group()

        self.all_sprites = pygame.sprite.Group()

        self.door = Door(0, 0)  # 出口初始化
        self.door_person = Door_Person(self.door.rect.x, self.door.rect.y)  # 出口动画初始化
        self.reached_door = False  # 新增碰撞状态标志

    def Start(self):
        pygame.init()  # 初始化pygame
        pygame.display.set_caption("门险环生")

        # 设置窗口图标
        icon = pygame.image.load(self.resource_path("source/img/icon.ico"))  # 推荐使用 32x32 或 64x64 的 .ico 文件
        pygame.display.set_icon(icon)  # 👈 这里设置窗口图标

        pygame.key.stop_text_input()

    #获取事件
    def GetEvent(self):
        for event in pygame.event.get():
            self.check_mute_click(event)
            if event.type == pygame.QUIT:
                self.ExitGame()

            elif event.type == pygame.KEYDOWN:
                if event.key == pygame.K_ESCAPE:
                    self.ExitGame()

    def update(self, dt):
        if self.person.alive and not self.reached_door:
            keys = pygame.key.get_pressed()
            self.person.move(keys, dt)
            self.person.update(self.obstacles, self.D_obstacles)
            self.person.per_img(dt)
            self.person.check_d_wall(self.D_Walls, self.window)
            self.person.check_die(self.Spikes, self.One_spike, self.Many_spike, self.Gear)
        else:
            # 强制停止人物移动
            self.person.vx = 0
            self.person.vy = 0
        # 先更新动态墙壁
        for d_wall in self.D_Walls:
            d_wall.update(self.window)  # 确保传递必要参数
        for spike in self.Spikes:
            spike.spikes_move(self.person, 'y', -5, 70)
        for gear in self.Gear:
            gear.gear_move()
    def render(self):
        self.window.fill(self.BackGroundColor)
        for gear in self.Gear:
            gear.Gear_display(self.window)

        for spike in self.Spikes:
            spike.move_img_display(self.window)
        self.obstacles.draw(self.window)
        for many_spike in self.Many_spike:
            many_spike.rect.left = self.D_Walls[0].rect.right
            self.window.blit(many_spike.image, many_spike.rect)
        for spike in self.One_spike:
            spike.spike_move()
            self.window.blit(spike.image,spike.rect)

        self.draw_mute_button()  # 绘制静音按钮

    def brith(self):
        if self.person.alive==False:
            self.__init__()

    def draw_mute_button(self):
        current_icon = self.unmute_icon if self.music_manager.muted else self.mute_icon
        self.window.blit(pygame.transform.scale(current_icon, (40, 40)), self.mute_btn)

    def check_mute_click(self, event):
        if event.type == pygame.MOUSEBUTTONDOWN:
            if self.mute_btn.collidepoint(event.pos):
                self.music_manager.toggle_mute()
                return True
        return False
    def resource_path(self,relative_path):
        """ 根据打包后的环境动态加载资源路径 """
        if hasattr(sys, '_MEIPASS'):
            base_path = sys._MEIPASS
        else:
            base_path = os.path.abspath(".")
        return os.path.join(base_path, relative_path)

    #结束游戏
    def ExitGame(self):
        pygame.quit()
        sys.exit()